﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.Model.Core
{
    public class Stock
    {
        List<Ingredient> ingredients;
        StorageHandler realStorage;
        StarDataDataContext db;

        public Stock(StarDataDataContext dataContext)
        {
            this.db = dataContext;

            ingredients = new List<Ingredient>();
            realStorage = new StorageHandler();
            LoadStorage();
        }
        public StorageHandler CloneStorage()
        {
            return realStorage.Clone();
        }
        private void LoadStorage()
        {
            foreach (var topping in db.Topping)
            {
                RegisterIngredient(topping, amount: topping.ToppingStorage.Amount);
            }
            foreach (var dough in db.Dough)
            {
                RegisterIngredient(dough, amount: dough.DoughStorage.Amount);
            }
        }
        private void RegisterIngredient(Ingredient ingredient, int amount)
        {
            ingredients.Add(ingredient);
            realStorage.SetAmount(ingredient, amount);
        }
        public bool AddNewTopping(Topping topping)
        {
            RegisterIngredient(topping, amount: 0);
            db.Topping.InsertOnSubmit(topping);
            //need to separate submission because of dependant ToppingID later:
            bool success = Utility.TrySubmitChanges(db);

            if (success)
            {
                db.ToppingStorage.InsertOnSubmit(
                    new ToppingStorage
                    {
                        Topping = topping,
                        Amount = realStorage.GetAmount(topping)
                    });
                success = Utility.TrySubmitChanges(db);
            }
            if (!success)
            {
                //TODO we have got a serious issue
            }
            return success;
        }
        public bool AddNewDough(Dough dough)
        {
            RegisterIngredient(dough, amount: 0);
            db.Dough.InsertOnSubmit(dough);
            db.DoughStorage.InsertOnSubmit(
                new DoughStorage
                {
                    Dough = dough,
                    Amount = realStorage.GetAmount(dough)
                });
            bool success = Utility.TrySubmitChanges(db);
            if (!success)
            {
                //TODO we have got a serious issue
            }
            return success;
        }
        public void PerformStorageChange(StorageHandler referenceStorage)
        {
            realStorage.RegisterDiff(referenceStorage);
            foreach (var toppingStorage in db.ToppingStorage)
            {
                toppingStorage.Amount = realStorage.GetAmount(toppingStorage.Topping);
            }
            foreach (var doughStorage in db.DoughStorage)
            {
                doughStorage.Amount = realStorage.GetAmount(doughStorage.Dough);
            }
            Utility.TrySubmitChanges(db);
        }
        public IEnumerable<Topping> GetToppings()
        {
            return db.Topping.ToList();
        }
        public IEnumerable<Dough> GetDoughs()
        {
            return db.Dough.ToList();
        }
    }
}
